<template>
<el-dialog :title="title"  :visible.sync="show"
  @close="close"
  >
  <el-dialog :title="'重置密码'"
  :visible.sync="pwshow"
  @close="hidepw"
  append-to-body
  top="25vh"
  >
  <el-form
  ref="form2"
  label-width="120px"
  :model="userpw"
  label-position="left"
  :rules="pwrule"
    >
  <el-form-item label="输入密码" prop="input" >
     <el-input placeholder="请输入密码" v-model="userpw.input"></el-input>
  </el-form-item>
  <el-form-item label="再次输入密码" prop="check">
  <el-input placeholder="再次输入密码" v-model="userpw.check"></el-input>
  </el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button type="primary" @click="submitForm('form2')"  class="confirm">确 定</el-button>
</div>
</el-dialog>
  <el-form ref="form" label-width="120px" :model="userinfo" label-position="left" >
  <el-form-item label="用户真实姓名 " >
      {{userinfo.realName}}
  </el-form-item>
  <el-form-item label="用户账号">
    {{userinfo.userName}}
  </el-form-item>
    <el-form-item label="用户higo号">
     {{userinfo.erbanNo}}
  </el-form-item>
      <el-form-item label="代理类型">
     {{userinfo.agentTyoe}}
  </el-form-item>
    <el-form-item label="用户密码" >
     密码已经设定,如果不慎忘记,可以帮其<el-button type="text" @click="showpw">重置密码</el-button>
  </el-form-item>
  <el-form-item label="状态设置" >
    <el-radio-group v-model="change.status">
      <el-radio  :label="1">正常使用</el-radio>
      <el-radio  :label="0">停用</el-radio>
    </el-radio-group>
  </el-form-item>
</el-form>
    <div slot="footer" class="dialog-footer">
    <el-button type="primary" @click="changeStatus" class="confirm">确 定</el-button>
    </div>

</el-dialog>

</template>

<script>
import { resetPassword,editagentMall } from "../../../Util/request";
  export default {
      data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.userpw.input !== '') {
            this.$refs.form2.validateField('check');
          }
          callback();
        }
      };
     var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.userpw.input) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
          return {
              show:false,
              pwshow:false,
              change:{
                  status:false,
              },
              userpw:{
                  input:"",
                  check:""
              },
              pwrule:{
                  input:[{validator: validatePass, trigger: 'blur'}],
                  check:[{ validator: validatePass2, trigger: 'blur' }]
              }

          }
      },
    props:{
        showpop:Boolean,
        title:  String,
        userinfo:Object,
        usermallId:Number
    },
    created(){
        console.log(this.userinfo.status,"这是用户状态");

    },
    methods: {
        close(){
          
            // console.log(this.show);
            console.log("修改用户被关闭");
            this.$emit("close")
            this.$emit("refresh")
            this.change.status= this.userinfo.status
        },
        showpw(){
            this.pwshow=true
        },
        hidepw(){
              
            this.pwshow=false
        },
       submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
          //修改密码
           resetPassword(res=>{
             if(res.code==200){
                     this.$message({
                    message: '密码修改成成功',
                    type: 'success'
                });
             }else{
                this.$message({
                    message: res.msg,
                    type: 'error'
                });
             }
             
             this.hidepw()
           },{
             mallId:this.userinfo.id,
             newPassword:this.userpw.check,
            
           })




          } else {
               this.$message({
                 message: '请核对密码输入是否一致',
                 type: 'warning'
               });
            return false;
          }
        });
      },
      changeStatus(){
        editagentMall((res)=>{
            if(res.code==200){
              this.$message({
                message:'修改成功',
                type:"sucess"
              })
            }else{
               this.$message({
                message:res.msg,
                type:"error"
              })
            }
            this.$emit("close")
        },{
          status:this.change.status,
          id:this.userinfo.id
        })
      }
    },
    watch:{
        showpop(){
            if(this.showpop){             
                this.show=true
            }else{
                this.show =false
            }
        },
        userinfo:{
            deep:true,
            handler(n,o){
            this.change.status = n.status
            this.change.diamonstatus=n.diamonstatus||false
    
            }
        },
 
    }
  };
</script>

<style lang="less">
    .el-dialog__header{
        height: 100px;
        // background: red;
        padding: 0;
        position: relative;
        height: 24px;
        .el-dialog__title{
            position: absolute;
            top: 0;
            left: 0;
            color: #848484;
            background: #f8f8f8;
            width: 100%;
            font-size: 15px;
        }
        .el-dialog__headerbtn{
            position: absolute;
            top: 5px;
            right: 5px;
        }
    }
    .el-dialog__footer{
        position: relative;
        width: 100%;
        height: 100px;
        padding: 0;
        // background: red;
        .confirm{
            position: absolute;
            top: 0;
            left: 50%;
            transform: translateX(-50%);
            width: 120px;
            
        }
    }
</style>